package com.ygp.asyn.util;

import com.ygp.asyn.enums.TimeFormatEnum;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;

public class TimeUtil {
    private static final String START_TIME = " 00:00:00";
    private static final String END_TIME = " 23:59:59";


    public static String stampToStr(Long stamp, TimeFormatEnum timeFormatEnum) {
        if (stamp == null) {
            return null;
        }
        LocalDateTime localDateTime = Instant.ofEpochMilli(stamp).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
        return ldtToStr(localDateTime, timeFormatEnum);
    }

    public static String nowToStr(TimeFormatEnum timeFormatEnum) {
        return DateTimeFormatter.ofPattern(timeFormatEnum.getFormat()).format(LocalDateTime.now());
    }

    public static String ldtToStr(LocalDateTime localDateTime, TimeFormatEnum timeFormatEnum) {
        if (localDateTime == null) {
            return null;
        }
        return DateTimeFormatter.ofPattern(timeFormatEnum.getFormat()).format(localDateTime);
    }

    public static String strLdAddDays(String date, TimeFormatEnum timeFormatEnum, int day) {
        LocalDate localDate = strToLd(date, timeFormatEnum);
        return localDate.plusDays(day).toString();
    }


    public static String ldToStr(LocalDate localDate, TimeFormatEnum timeFormatEnum) {
        if (localDate == null) {
            return null;
        }
        return DateTimeFormatter.ofPattern(timeFormatEnum.getFormat()).format(localDate);
    }

    public static String ldtToEndStr(LocalDateTime localDateTime) {
        if (localDateTime == null) {
            return null;
        }
        java.time.LocalDate localDate = localDateTime.toLocalDate();
        return localDate.toString() + END_TIME;
    }

    public static String ldtToStartStr(LocalDateTime localDateTime) {
        if (localDateTime == null) {
            return null;
        }
        java.time.LocalDate localDate = localDateTime.toLocalDate();
        return localDate.toString() + END_TIME;
    }

    public static LocalDateTime ldtToEndLdt(LocalDateTime localDateTime) {
        if (localDateTime == null) {
            return null;
        }
        java.time.LocalDate localDate = localDateTime.toLocalDate();
        String start = localDate.toString() + END_TIME;
        return LocalDateTime.parse(start, DateTimeFormatter.ofPattern(TimeFormatEnum.YYYY_2.getFormat()));
    }

    public static LocalDateTime ldtToStartLdt(LocalDateTime localDateTime) {
        if (localDateTime == null) {
            return null;
        }
        java.time.LocalDate localDate = localDateTime.toLocalDate();
        String start = localDate.toString() + START_TIME;
        return LocalDateTime.parse(start, DateTimeFormatter.ofPattern(TimeFormatEnum.YYYY_2.getFormat()));
    }

    /**
     * 计算两个字符串日期之间的间隔
     *
     * @param start          开始时间
     * @param end            截止时间
     * @param timeFormatEnum 日期格式
     * @param chronoUnit     转换对象
     * @return ；
     */
    public static long calBetween(String start, String end, TimeFormatEnum timeFormatEnum, ChronoUnit chronoUnit) {
        LocalDateTime startTm = LocalDateTime.parse(start, DateTimeFormatter.ofPattern(timeFormatEnum.getFormat()));
        LocalDateTime endTm = LocalDateTime.parse(end, DateTimeFormatter.ofPattern(timeFormatEnum.getFormat()));
        return chronoUnit.between(startTm, endTm);
    }

    /**
     * 计算两个字符串日期之间的间隔
     *
     * @param tmStr          字符串时间
     * @param timeFormatEnum 日期格式
     * @return ；
     */
    public static LocalDateTime trans(String tmStr, TimeFormatEnum timeFormatEnum) {
        if (tmStr == null) {
            return null;
        }
        return LocalDateTime.parse(tmStr, DateTimeFormatter.ofPattern(timeFormatEnum.getFormat()));
    }

    public static LocalDate strToLd(String tmStr, TimeFormatEnum timeFormatEnum) {
        if (tmStr == null) {
            return null;
        }
        return LocalDate.parse(tmStr, DateTimeFormatter.ofPattern(timeFormatEnum.getFormat()));
    }

    public static List<String> hourList(int startHour, int endHour) {
        ArrayList<String> list = new ArrayList<>();
        for (int i = startHour; i <= endHour; i++) {
            list.add(i >= 10 ? (i + "") : ("0" + i));
        }
        return list;
    }


    /**
     * 日期转整数
     *
     * @param localDate ;
     * @return ;
     */
    public static Integer dayToInt(LocalDate localDate) {
        if (localDate == null) {
            return null;
        }
        return Integer.parseInt(localDate.toString().replaceAll("-", ""));
    }


    public static String ldtToHour00(LocalDateTime ldt) {
        if (ldt == null) {
            return null;
        }
        return DateTimeFormatter.ofPattern(TimeFormatEnum.HH.getFormat()).format(ldt);
    }

    public static String ldtToHour24(LocalDateTime ldt) {
        String hh = ldtToHour00(ldt);
        if ("00".equals(hh)) {
            return "24";
        } else {
            return hh;
        }
    }

    public static String addHour00(String hh, int hours) {
        if (hh == null) {
            return null;
        }
        int newHour = Integer.parseInt(hh) + hours;
        newHour = newHour % 24;
        if (newHour < 0) {
            newHour += 24;
        }
        return (newHour > 9 ? "" : "0") + newHour;
    }

    public static String addHour24(String hh, int hours) {
        String hour = addHour00(hh, hours);
        if ("00".equals(hour)) {
            return "24";
        } else {
            return hour;
        }
    }

    public static String parseHour(LocalDateTime localDateTime) {
        if (localDateTime == null) {
            return null;
        }
        int hour = localDateTime.getHour();
        return hour > 9 ? ("" + hour) : ("0" + hour);
    }

    public static String parseNowHour() {
        LocalDateTime now = LocalDateTime.now();
        int hour = now.getHour();
        return hour > 9 ? ("" + hour) : ("0" + hour);
    }

}
